/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function submitForm(form, action)
{
	form.acao.value = action;
	if (action == 'excluir')
	{
		if (excluir(form))
                {
                    if (form.name == 'listaPac')
                    {
                        excluirForm('page', 'paciente/acao.php', form);
                    }
                    else if (form.name == 'listaEstab')
                    {
                        excluirForm('page', 'estabelecimento/acao.php', form);
                    }
                    else if (form.name == 'listaDesp')
                    {
                        excluirForm('page', 'despesa/acao.php', form);
                    }
                    else if (form.name == 'listaTipoDesp')
                    {
                        excluirForm('page', 'tipoDespesa/acao.php', form);
                    }
                    else if (form.name == 'listaRecibo')
                    {
                        excluirForm('page', 'recibo/acao.php', form);
                    }
                    else if (form.name == 'listaAut')
                    {
                        excluirForm('page', 'autorizacao/acao.php', form);
                    }
            //                    document.getElementById(form.name).submit();
                }
	}
      else if (action == 'imprimir')
        {
            if (form.name == 'geraRelatDesp')
            {
                if (validateFormGeraRelatDesp(form)) {
//			    document.getElementById(form.name).submit();
			var radioObj = form.tipo;
			var radioLength = radioObj.length;
			var tipo = "";

			if(radioLength == undefined)
				if(radioObj.checked)
					tipo = radioObj.value;
				else
					tipo = "";

			for(var i = 0; i < radioLength; i++) {
				if(radioObj[i].checked)
					tipo = radioObj[i].value;
			}
                  window.open('despesa/relatorio.php?tipo='+tipo+'&idPac='+form.idPac.value+'&idEstab='+form.idEstab.value+'&idTipoDesp='+form.idTipoDesp.value+'&dataIni='+form.dataIni.value+'&dataFim='+form.dataFim.value, 'relatorio', 'status=0, toolbar=0, location=0, menubar=1, resizable=1');
                }
		    else
                  return false;
            }
      }
	else
	{
		if (form.name == 'cadClie')
		{
			if (validateFormPac(form))
                            saveForm('page', 'paciente/acao.php', form);
//				document.getElementById(form.name).submit();
			else
				return false;
		}
		else if (form.name == 'cadEstab')
		{
			if (validateFormEstab(form))
                            saveForm('page', 'estabelecimento/acao.php', form);
//				document.getElementById(form.name).submit();
			else
				return false;
		}
		else if (form.name == 'cadTipoDesp')
		{
			if (validateFormTipoDesp(form))
                            saveForm('page', 'tipoDespesa/acao.php', form);
//				document.getElementById(form.name).submit();
			else
				return false;
		}
		else if (form.name == 'cadDesp')
		{
			if (validateFormDesp(form))
                            saveForm('page', 'despesa/acao.php', form);
//				document.getElementById(form.name).submit();
			else
				return false;
		}
		else if (form.name == 'cadRec')
		{
			if (validateFormRec(form))
                            saveForm('page', 'recibo/acao.php', form);
//				document.getElementById(form.name).submit();
			else
				return false;
		}
		else if (form.name == 'cadAut')
		{
			if (validateFormAut(form))
                            saveForm('page', 'autorizacao/acao.php', form);
//				document.getElementById(form.name).submit();
			else
				return false;
		}
		else if (form.name == 'bdInstall')
		{
			if (validateFormBDInstall(form))
                            saveForm('page', 'acao.php', form);
//				document.getElementById(form.name).submit();
			else
				return false;
		}
        }
        return true;
}

function excluir(form)
{
	var temUmSelecionado = false;

	for (var x=0; x<form.selecionar.length; x++)
	{
		if (form.selecionar[x].checked == true)
		{
			temUmSelecionado = true;
		}
	}

	if (temUmSelecionado == true || form.selecionar.checked == true)
	{
		if (confirm("Tem certeza que deseja excluir os itens selecionados?"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert('Primeiro selecione pelo menos um item!');
		return false;
	}

	return false;
}
